<template>
  <!-- 租户管理员管理 -->
  <div class="TenantInformationManagement">
    <div style="padding: 0px 0px;">
      <!-- {{dataModel}} -->
      <!-- 搜索 -->
      <!-- <SearchCForL
        :searchData="dataModel"
        @searchCallBack="searchCallBack"
      ></SearchCForL> -->
      <!-- {{searchContent}} -->
      <searchComponentsCss :searchData="dataModel" @searchCallBack="searchCallBack" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :shrinkBoxOpenClose='shrinkBoxOpenClose' :getFindByTypeData="getFindByTypeData"></searchComponentsCss>

      <!-- 收缩框 -->
      <!-- {{numSearch}} -->
      <div class="shrinkBox" @click="shrinkBoxOpenClose = !shrinkBoxOpenClose">
        <i :class="shrinkBoxOpenClose?'el-icon-arrow-up':'el-icon-arrow-down'" v-if="numSearch>8"></i>
      </div>
      <!-- @clearSearch='clearSearch' -->
      <div style="border-top:1px solid #E6E6E6;margin:0 30px;"></div>
      <div class="right_row" style="padding: 25px 30px;">
        <!--添加和批量删除组件-->

        <addComponent class="buttonhurdle" style="margin-left: 0px;" :diaDataAdd="dataModel" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :getFindByTypeData="getFindByTypeData" :sysCompanyCodeRoles="sysCompanyCodeRoles" @toAdd="toAdd" @toDelete="toDelete" @sysRoleId="sysRoleId"></addComponent>

        <el-button type="primary" plain @click="resetPassWord" size="mini" style="margin-left: 15px;" icon="el-icon-refresh">重置密码</el-button>
        <!--<el-button  style="float:right;margin-right:30px;" type="primary" size="mini" @click="sunS_F">搜索</el-button>
         <buttonhurdle
          class="buttonhurdle"
          :diaDataAdd="dataModel"
          :optionsZi="optionsZi"
          :searchOptionTable="searchOptionTable"
          :getFindByTypeData="getFindByTypeData"
          :sysCompanyCodeRoles="sysCompanyCodeRoles"
          @toAdd="toAdd"
          @toDelete="toDelete"
          @sysRoleId="sysRoleId"
        ></buttonhurdle> -->
        <div class="buttons">
          <el-button style="float:right;" type="primary" size="mini" plain @click="sunS_F" icon="el-icon-search">搜索</el-button>
        </div>
        <!-- <el-button
            type="primary"
            size="mini"
            plain
            icon="el-icon-refresh-right"
            @click="emptySearch"
          >重置</el-button>
          <el-button
            type="primary"
            size="mini"
            plain
            icon="el-icon-notebook-2"
          >配置</el-button>
        </div> -->
      </div>

    </div>
    <div class="out" style="padding:0px 30px">
      <!-- 字典 -->
      <dictionaries :searchData='dataModel' :modelType='modelType' @optionData="dictionariesCallBack"></dictionaries>
      <!--数据列表-->
      <!-- {{dataAttribute}} -->
      <el-table ref="multipleTable" :data="dataAttribute" @selection-change="handleSelectionChange" v-loading.body="false" border fit max-height="511px" highlight-current-row size="mini" :cell-style="{padding:'5px 0'}">
        <el-table-column align="center" type="selection" :show-overflow-tooltip="true" width="45"></el-table-column>
        <el-table-column align="center" label="序号" type="index" :index="indexMethod" width="54px">
        </el-table-column>
        <el-table-column align="center" min-width="140px" v-for="item in dataModel" v-if="item.tableSign" :key="item.id" :label="item.CN" :show-overflow-tooltip="true">
          <!-- <template v-if="item.EN=='sysCompanyCode'" >
            <template slot-scope="scope">{{scope.row[item.EN]}}</template>
            &&item.EN!='sysCompanyCode'
          </template> -->
          <template slot-scope="scope">{{item.type === 'select' ? optionDatas(item.typeGroupCode?optionsZi[item.EN]:searchOptionTable[item.EN],scope.row[item.EN],item.EN):scope.row[item.EN]}}</template>
          <!-- <template
              slot-scope="scope"
            >{{item.type === 'select' ? optionDatas(item.typeGroupCode?optionsZi[item.EN]:searchOptionTable[item.EN],scope.row[item.EN]):scope.row[item.EN]}}</template>       -->
        </el-table-column>
        <el-table-column width="180px" align="center" label="操作">
          <template slot-scope="scope">
            <!-- 查看按钮 -->
            <el-button style="color:#0B9AC3;border:none;" icon="el-icon-view" circle @click="makeExaId(scope.row)"></el-button>
            <!-- 修改组件 -->
            <!-- {{dataModel}} -->
            <!-- {{modifyData}} -->
            <!-- {{getFindByTypeData}} -->
            <modificationi class="inline_block" :attributeOrder="dataModel" :modData="scope.row" @modifyData="modifyData" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :getFindByTypeData="getFindByTypeData" @sysRoleIdq="sysRoleIdq" @modificationiButtonClick='modificationiButtonClick'></modificationi>
            <!-- :roleId="roleId(scope.row)"    -->
            <!-- :modData="scope.row" -->
            <!-- 删除按钮 -->
            <el-button style="color:#C90010;border:none;" icon="el-icon-delete" circle @click="deleteList(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination style="margin-top:15px;" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="pagetotal"></el-pagination>
    </div>
    <!-- 查看组件 -->
    <examine :key="modificationiButtonKey" :DialogVisibleMod="DialogVisibleExamine" :modData="examineData" :attributeOrder="dataModel" @examineDialog="examineDialog" :optionsZi="optionsZi" :searchOptionTable="searchOptionTable" :getFindByTypeData="getFindByTypeData"></examine>
  </div>
</template>
<script>
//引入组件
import {
  SearchCForL,
  buttonhurdle,
  examine,
  searchComponents,
  searchComponentsCss,
  dictionaries,
} from '@/views/components/FLPage'
//引入组件
import modificationi from './compotents/modificationi'
//引入组件
import addComponent from './compotents/addComponent'
//引入api ajax
//引入API
import {
  postAddUserAndMember, //增加
  deleteByuserId, //单项删除
  searchPageSize, //分页查询
  putUpdateUserAndMember, //修改
  examineBaseTenantInformation, //查看
  forSABT, //查角色
  //不知道做什么的请求
  getselectAllByType,
  //根据ID获取
  FindByLevel, //查询租户管理员的租户
  FindByUserId, //根据用户id查询用户角色中间表
  SAllByType1, //获得用户
  getSelectByCodeAndType, //用于添加租户管理员时选择租户后之间传回选择租户的角色：
  findByType, //获取租户信息
  selectNameByuserId, //租户管理员编辑组件  查询角色
} from '@/api/TenantAdministratorManagement'
import {
  returnPass, //重置密码
} from '@/api/admin/user/index'
export default {
  components: {
    buttonhurdle,
    modificationi,
    examine,
    searchComponents,
    searchComponentsCss,
    SearchCForL,
    dictionaries,
    addComponent,
  },
  name: 'TenantInformationManagement',
  data() {
    return {
      numSearch: 0,
      //   收缩框开关
      shrinkBoxOpenClose: false,
      modificationiButtonKey: 0,
      // 角色Id
      roleIdData: null,
      // 编辑传回角色id
      roleIdDatass: null,
      // indexMethods:'',
      menuTrigger: '',
      //搜索
      searchContent: {},
      //后端传来的数据
      dataAttribute: [],
      //分页
      currentPage: 1, //当前页
      pagesize: 10, //每页条数
      pagetotal: 0, //总条数
      //查看的数据
      examineData: {},
      //查看的弹窗控制器
      DialogVisibleExamine: false,
      //表格中选中的数组
      multipleSelection: [],
      // 字典请求字段数据
      modelType: 'table',
      optionsZi: [],
      searchOptionTable: [],
      //数据模型
      dataModel: [
        {
          EN: 'username',
          CN: '账 户',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          // Exp: "^.{2,20}$",
          // Exp: "^(?=.*[0-9].*)(?=.*[A-Z].*)(?=.*[a-z].*).{6,20}$",
          Exp: '^[0-9A-Za-z.@-_]{4,16}$',
          err: '请输入4-16位且由数字和字母组成',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'password',
          CN: '密码',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$',
          // Exp: "^.{2,20}$",
          err: '6-20个字符 数字和字符组成',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: false,
          examineSign: false,
          examineDisabled: false,
          modificationSign: false,
        },
        {
          EN: 'name',
          CN: '用户名',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '^.{2,20}$',
          err: '6-20个字符 数字和字符组成',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'telPhone',
          CN: '手机号码',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '^1[0-9]{10}$',
          err: '2-10个字符',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'sysCompanyCode',
          CN: '租 户',
          value: '',
          type: 'select',
          option: [],
          typeFrom: 'string',
          Exp: '^.{1,100}$',
          err: '请选择租户',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'groupId',
          CN: '角  色',
          value: '',
          type: 'select',
          option: [],
          radio: [], //下面赋值
          Exp: '^.{1,100}$',
          err: '请选择角色',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: false,
          examineSign: false,
          examineDisabled: false,
          modificationSign: true,
        },
        {
          EN: 'status',
          CN: '状 态',
          value: '',
          type: 'select',
          option: [],
          typeFrom: 'string',
          Exp: '^.{1,100}$',
          err: '请选择状态',
          result: true,
          sign: true,
          searchSign: true,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
          typeGroupCode: 'statusDct',
        },
        {
          EN: 'description',
          CN: '描 述',
          value: '',
          type: 'input',
          option: [],
          typeFrom: 'string',
          Exp: '',
          err: '',
          result: true,
          sign: true,
          searchSign: false,
          addSign: true,
          tableSign: true,
          examineSign: true,
          examineDisabled: false,
          modificationSign: true,
          skipSign: true,
        },
      ],
      //角色
      ROLE: undefined,
      //用户角色
      useruData: [],
      // UserArr:undefined,
      // 改变的数据
      sysCompanyCodeRoles: '',
      // 租户信息
      getFindByTypeData: [],
      // 编辑中的角色默认值
      roleIdDataMod: '',
    }
  },
  // searchContent.sysCompanyCode
  created() {
    this.getFindByType()
    this.forData(this.currentPage, this.pagesize, this.searchContent)
  },
  methods: {
    // 搜索框
    getSSearchNum() {
      let searchData = this.searchData
      let num = 0
      for (let i = 0; i < searchData.length; i++) {
        const element = searchData[i]
        if (element.searchSign) {
          num += 1
        } else {
          num = num
        }
      }
      this.numSearch = num
    },
    async resetPassWord() {
      let multipleSelection = this.multipleSelection
      if (multipleSelection.length === 0) {
        this.$message({
          message: '请选择要重置的账号',
          type: 'warning',
        })
      } else if (multipleSelection.length > 1) {
        this.$message({
          message: '不支持批量重置密码',
          type: 'warning',
        })
      } else {
        // 发请求   给提醒
        let id = this.multipleSelection[0].id
        console.log(this.multipleSelection[0], 9966)
        const res = await returnPass(id)
        if (res.flag) {
          this.$message({
            message: '密码重置成功,密码为123456',
            type: 'success',
          })
        } else {
          this.$message({
            message: '密码重置失败',
            type: 'warning',
          })
        }
      }
    },
    modificationiButtonClick(newValue) {
      if (newValue == '点击编辑') {
        // this.forData(this.currentPage, this.pagesize);
        // this.modificationiButtonKey = this.modificationiButtonKey + 1
      }
    },
    // 请求获取默认的角色
    // 编辑传角色id
    sysRoleIdq(newId) {
      console.log(newId, '参数ID')
      this.roleIdDatass = newId
    },
    // 角色
    sysRoleId(newId) {
      console.log(newId, '参数ID')
      this.roleIdData = newId
    },
    // 获取租户信息
    async getFindByType() {
      // console.log('qian2222222222222222222222222222');
      const res = await findByType(0)
      // console.log(res,'hou2222222222222222222222222222222222');
      this.getFindByTypeData = res.data
      let getFindByTypeData = res.data
      console.log(this.getFindByTypeData, 99999666)
      // for (let i = 0; i < getFindByTypeData.length; i++) {
      //   const element = getFindByTypeData[i];
      //   this.getSelectByCodeAndTypeData(element.id)

      // }
    },
    // 用于添加租户管理员时选择租户后之间传回选择租户的角色：
    // async getSelectByCodeAndTypeData(sysCompanyCode){
    //   console.log(sysCompanyCode,999999999);

    //   const res = await getSelectByCodeAndType(sysCompanyCode)
    //   console.log(res,'用于添加租户管理员时选择租户后之间传回选择租户的角色');

    //   const optionDatas = res.data
    //   this.sysCompanyCodeRoles = res.data
    //   console.log(this.sysCompanyCodeRoles,'对应关系999999999996666');
    //   return res.data

    // },
    // 重置
    emptySearch() {
      this.searchContent = undefined
      this.forData(this.currentPage, this.pagesize)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    //查看关闭弹窗
    examineDialog(dialogVisibleMod) {
      this.DialogVisibleExamine = false
    },
    async searchContentMod(data) {
      for (let i = 0; i < data.length; i++) {
        const element = data[i]
        const res = await selectNameByuserId(element.id)
        // console.log(res,77777);
        if (res.data) {
          element.groupId = res.data.id
        }
      }
      this.dataAttribute = data
      console.log(data, 'gaihaode9999999999999999999999999999')
      // this.roleIdDataMod =
      // 整理groupId
      // selectNameByuserId()
    },
    //分页查询
    forData(page, size, content) {
      /*  if (!Object.keys(content).length) {
        this.$message({
          message: '查询条件不为空',
          type: 'warning',
        })
        return
      } */
      searchPageSize(page, size, content).then((response) => {
        this.dataAttribute = response.data.list
        // 整理数据
        this.searchContentMod(this.dataAttribute)
        console.log(this.dataAttribute, 85263)

        this.pagetotal = response.data.total
      })
    },
    //分页
    handleSizeChange(val) {
      console.log('每页 ' + val + ' 条')
      this.pagesize = val
      this.forData(this.currentPage, val, this.searchContent)
    },
    handleCurrentChange(val) {
      console.log('当前页: ' + val)
      // 序号
      // this.indexMethod()
      this.forData(val, this.pagesize, this.searchContent)
      this.currentPage = val
    },
    //查询组件回调
    searchCallBack(newData) {
      let obj = {}
      for (let i = 0; i < newData.length; i++) {
        obj[newData[i].EN] = newData[i].value
      }
      // console.log(obj)
      this.searchContent = obj
    },
    //搜索
    sunS_F() {
      //console.log(this.searchContent); //用这组数据去ajax查询
      this.currentPage = 1
      this.forData(this.currentPage, this.pagesize, this.searchContent)
    },
    //增加
    toAdd(toAddData) {
      // console.log("toAddData");
      console.log(toAddData, 9999)

      let obj = {}
      for (let i = 0; i < toAddData.length; i++) {
        obj[toAddData[i].EN] = toAddData[i].value
      }
      let roleIdData = obj.groupId
      // console.log(obj,roleIdData,'????????????');
      // console.log(roleIdData,'roleIdData');
      console.log(obj, roleIdData)

      postAddUserAndMember(obj, roleIdData)
        .then((response) => {
          this.forData(this.currentPage, this.pagesize, this.searchContent)
          this.$message({
            message: response.message,
            type: 'success',
          })
        })
        .catch(() => {
          this.$message.error('添加失败')
        })
    },
    //批量删除
    toDelete(toDeleteData) {
      console.log(toDeleteData)
      console.log(this.multipleSelection)
      let arr = []
      for (let i = 0; i < this.multipleSelection.length; i++) {
        arr.push(this.multipleSelection[i].id)
        this.$confirm('确认批量删除？')
          .then((_) => {
            deleteBaseTenantInformation(arr).then((response) => {
              this.forData(this.currentPage, this.pagesize, this.searchContent) //刷新列表
            })
            done()
          })
          .catch((_) => {})
      }
    },
    //单项删除
    deleteList(id) {
      console.log(id)
      let arrId = [id]
      this.$confirm('确认删除？')
        .then((_) => {
          deleteByuserId(arrId).then((response) => {
            this.forData(this.currentPage, this.pagesize, this.searchContent) //刷新列表
            this.$message({
              type: 'success',
              message: '已删除',
            })
          })
        })
        .catch((_) => {
          // this.$message({
          //   type:'warning',
          //   message:'已取消删除'
          // })
        })
    },
    //查看
    makeExaId(row) {
      let copyDialog = true
      this.DialogVisibleExamine = copyDialog
      this.examineData = row
    },
    //表格选中事件
    handleSelectionChange(val) {
      this.multipleSelection = val
      //传递删除的参数
      this.deleteDataList = val
    },
    //修改
    modifyData(value, MDData, groupid) {
      console.log(value, MDData, 'sss', 'groupid----', groupid)
      if (value) {
        let id = MDData.id
        console.log(this.roleIdDatass, 'sssss9999')
        const roleIdDatass = this.roleIdDatass
        // var roleIdDatass = null
        // this.$nextTick(function(){
        //   roleIdDatass = this.roleIdDatass
        // })
        // const roleIdDatass = this.roleIdDatass
        // putUpdateUserAndMember
        console.log(roleIdDatass, id, 6666)
        console.log(groupid, 336699)

        putUpdateUserAndMember(groupid, id, MDData)
          .then((Response) => {
            console.log(Response, 88855555)

            this.forData(this.currentPage, this.pagesize, this.searchContent)
            // this.$message({
            //   message: "修改成功!",
            //   type: "success"
            // });
          })
          .catch(() => {
            this.$message.error(
              '错误信息：api/admin/putUpdateUserAndMember/${id}！'
            )
          })
      }
    },
    // 字典组件回调
    dictionariesCallBack([a = {}, b = {}]) {
      // 正常字典
      this.optionsZi = a
      // 跨表字典
      this.searchOptionTable = b
      console.log(a, b, 66688)
      // console.log(options, 111)
      // console.log(options,'huilaide');
      // this.optionsZi = options
      this.currentPage = 1
      this.forData(this.currentPage, this.pagesize, this.searchContent)
      // }
    },
    // 序号
    indexMethod(index) {
      var currentPage = this.currentPage
      var pagesize = this.pagesize
      this.$nextTick(function () {
        currentPage = this.currentPage
        pagesize = this.pagesize
      })
      // console.log(currentPage,pagesize,'996');
      // console.log(currentPage*pagesize+index - 9);
      return currentPage * pagesize - pagesize + 1 + index
    },
  },
  computed: {
    // 字典计算属性
    optionDatas() {
      return function (arr, val, EN) {
        // console.log(arr,val,EN,'99999');

        // console.log(arr[val],'why');
        // 判断特殊
        if (EN == 'sysCompanyCode') {
          const getFindByTypeData = this.getFindByTypeData
          // console.log(getFindByTypeData,9999999999);

          for (let i = 0; i < getFindByTypeData.length; i++) {
            const element = getFindByTypeData[i]
            if (getFindByTypeData[i].sysCompanyCode == val) {
              return getFindByTypeData[i].name
            }
          }
        }
        if (arr) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].value == val) {
              // console.log(arr[i].label)
              return arr[i].label
            }
          }
        }
      }
    },
  },
  // watch:{
  //   searchContent(newValue){
  //     console.log(newValue.sysCompanyCode,'??');
  //     const sysCompanyCode = newValue.sysCompanyCode
  //     this.getSelectByCodeAndTypeData(sysCompanyCode)
  //   }
  // }
}
</script>
<style scoped src="@/styles/allCssP.css"></style>
<style lang="scss" scoped>
.TenantInformationManagement {
  .shrinkBox {
    text-align: center;
    font-weight: 700;
    font-size: 20px;
    height: 25px;
    // border-bottom:1px solid #E6E6E6;
  }
  /deep/ .el-table--mini {
    .gutter {
      display: table-cell !important;
    }
    .el-checkbox__inner {
      width: 12px;
      height: 12px;
    }
    // 改变表头的高度  改为45px
    /deep/.el-table__header-wrapper {
      .el-table__header {
        .has-gutter {
          tr {
            th {
              padding: 5px 0 !important;
              height: 34px;
              line-height: 34px;
            }
            // .is-leaf{
            //   padding: 5px 0 !important;

            // }
          }
        }
      }
    }
    // 表头操作固定定位
    /deep/.el-table__fixed-right {
      .el-table__header {
        thead {
          tr {
            th {
              padding: 5px 0 !important;
              height: 34px;
              line-height: 34px;
              .cell {
                // padding-bottom: 5px !important;
                //   height: 34px;
                //   line-height: 34px;
              }
            }
          }
        }
      }
    }
  }
  .right_row {
    display: flex;
    // justify-content: start;
    align-items: center;
    position: relative;

    .buttons {
      position: absolute;
      padding: 0px !important;
      right: 30px;
    }
    .buttonhurdle {
      padding: 0px !important;
    }
  }
  .do {
    display: flex;
  }
  .pagination {
    text-align: left;
    margin-top: 20px;
  }
}
</style>
